package com.onesports.intelligent.k12.polarlight.service.wx;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
import com.onesports.intelligent.k12.polarlight.config.ApplicationProperties;
import com.onesports.intelligent.k12.polarlight.domain.dto.wx.WxShareCodeDTO;
import com.onesports.intelligent.k12.polarlight.domain.dto.wx.WxShareUrlLinkDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * 微信分享服务
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class WxShareService {

    private final WxMaService wxMaService;

    private final ApplicationProperties properties;

    public String generateUrlLink(WxShareUrlLinkDTO dto) throws WxErrorException {
        GenerateUrlLinkRequest request = new GenerateUrlLinkRequest();
        request.setEnvVersion(StringUtils.trimToNull(properties.getWx().getMiniapp().getEnvVersion()));
        if(StringUtils.isNotBlank(dto.getPath())){
            request.setPath(StringUtils.trimToNull(dto.getPath()));
        }
        if(StringUtils.isNotBlank(dto.getQuery())){
            request.setQuery(StringUtils.trimToNull(dto.getQuery()));
        }
        return wxMaService.getLinkService().generateUrlLink(request);
    }
}
